package com.daihou.BCJ.decrease;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.daihou.BCJ.repayment.BalanceRepayment;
import com.daihou.config.TheAuth;
import com.daihou.model.ceres.DecreaseUrlEnum;
import com.daihou.utils.DHConfigFile;
import com.daihou.utils.DHHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.*;

/**
 * @author liujintao
 * @date 2021/2/8 10:37
 */
public class Classify {
    private static final Logger logger = LoggerFactory.getLogger(BalanceRepayment.class);
    private String url;
    Map<String, String> headers = new HashMap<>();
    private int id;

    @BeforeTest(description = "测试准备工作,获取Authorization对象")
    public void beforeTest() throws IOException {
        url = DHConfigFile.getUrl("DaiHou_URL", "decrease", DecreaseUrlEnum.CLASSIFYADD.toString());
        //设置请求头中的Authorization
        headers.put("Authorization", TheAuth.getAuth());
        headers.put("X-UserName", "admin");
    }

    @Test(description = "减免分类列表新增")
    public void classifyAddTest() {
        String body = "{\"id\":\"\",\"code\":[\"COMMUTATION\"]}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("减免分类列表新增结果：{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }

    @Test(description = "减免分类列表查询", dependsOnMethods = "classifyAddTest")
    public void classifyListQueryTest() {
        url = DHConfigFile.getUrl("DaiHou_URL", "decrease", DecreaseUrlEnum.CLASSIFYQUERY.toString());
        String body = "{\"pageNum\":1,\"pageSize\":20,\"order\":null,\"id\":\"\",\"code\":[\"COMMUTATION\"]}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("减免分类列表查询结果：{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
        List<String> rs = new ArrayList<>();
        rs.add("COMMUTATION");
        JSONArray resultJson = JSON.parseObject(result).getJSONObject("data").getJSONArray("list");
        for (int i = 0; i < resultJson.size(); i++) {
            JSONObject ss = resultJson.getJSONObject(i);
            List<String> codeList = ss.getObject("code", new TypeReference<List<String>>() {
            });
            if (rs.containsAll(codeList)) {
                id = ss.getIntValue("id");
                break;
            }
        }
        logger.info("id值：{}", id);
    }

    @Test(description = "减免分类列表更新", dependsOnMethods = "classifyListQueryTest")
    public void classifyListUpdateTest() {
        url = DHConfigFile.getUrl("DaiHou_URL", "decrease", DecreaseUrlEnum.CLASSIFYUPDATE.toString());
        String body = "{\"id\":" + id + ",\"code\":[\"COMMUTATION\"]}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("减免分类列表查询结果：{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }

    @Test(description = "减免分类列表删除", dependsOnMethods = "classifyListUpdateTest")
    public void classifyListDeleteTest() {
        url = DHConfigFile.getUrl("DaiHou_URL", "decrease", DecreaseUrlEnum.CLASSIFYDELETE.toString());
        String body = "{\"id\":" + id + "}";
        String result = Objects.requireNonNull(DHHttpUtil.post_with_String(url, body, headers)).toJSONString();
        logger.info("减免分类列表查询结果：{}", result);
        Assert.assertTrue(result.contains("\"message\":\"操作成功\",\"status\":\"0\""));
    }
}